package cn.mcch.investigator.customview;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import cn.mcch.investigator.R;
import cn.mcch.investigator.adapter.base.CommonAdapter;
import cn.mcch.investigator.adapter.base.ViewHolder;
import cn.mcch.investigator.model.bean.CarType;
import cn.mcch.investigator.utils.ConstantUtil;
import cn.mcch.investigator.utils.DensityUtils;

/**
 * Created by Administrator on 2016/12/20.
 */
public class IndexExpandableListView extends RelativeLayout implements ExpandableListView.OnGroupClickListener {

    private ExpandableListView elv;

    private LinearLayout ll_index;

    private TextView tvIndexChar;

    private List<String> indexList;

    public IndexExpandableListView(Context context) {
        super(context);
    }

    public IndexExpandableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public IndexExpandableListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        // 添加一个ExpandableListView
        elv = new ExpandableListView(getContext());
        elv.setOnGroupClickListener(this);
        RelativeLayout.LayoutParams elvLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        elv.setLayoutParams(elvLayoutParams);
        this.addView(elv);

        // 添加索引条
        ll_index = new LinearLayout(getContext());
        RelativeLayout.LayoutParams ll_indexLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        ll_indexLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        ll_index.setLayoutParams(ll_indexLayoutParams);
        ll_index.setOrientation(LinearLayout.VERTICAL);
        ll_index.setBackgroundColor(getResources().getColor(R.color.index_bg));
        this.addView(ll_index);

        // 添加索引字符
        RelativeLayout.LayoutParams ll_indexCharLayoutParams = new RelativeLayout.LayoutParams(DensityUtils.dp2px(getContext(), 60), DensityUtils.dp2px(getContext(), 60));
        ll_indexCharLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        tvIndexChar = new TextView(getContext());
        tvIndexChar.setGravity(Gravity.CENTER);
        tvIndexChar.setTextColor(Color.WHITE);
        tvIndexChar.setTextSize(18);
        tvIndexChar.setVisibility(View.GONE);
        tvIndexChar.setLayoutParams(ll_indexCharLayoutParams);
        tvIndexChar.setBackgroundResource(R.drawable.shape_index_char_bg);
        this.addView(tvIndexChar);

    }


    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
        // 点击group不会收缩
        return true;
    }

    public IndexExpandableListView setIndex(List<String> index) {
        indexList = index;
        // 添加热门索引字符
        LinearLayout.LayoutParams paramsHot = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
        paramsHot.weight = 1;

        TextView textViewHot = new TextView(getContext());
        textViewHot.setLayoutParams(paramsHot);
        textViewHot.setPadding(DensityUtils.dp2px(getContext(), 6), 0, DensityUtils.dp2px(getContext(), 6), 0);
        textViewHot.setTextSize(12);
        textViewHot.setText("热门");
        textViewHot.setTextColor(Color.WHITE);
        textViewHot.setGravity(Gravity.CENTER);
        textViewHot.setClickable(true);
        textViewHot.setOnTouchListener(new IndexTouchListener(-1));
        ll_index.addView(textViewHot);
        // 添加索引字符
        for (int i = 0; i < index.size(); i++) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
            params.weight = 1;
            TextView textView = new TextView(getContext());
            textView.setLayoutParams(params);
//            textView.setPadding(DensityUtils.dp2px(getContext(), 6), 0, DensityUtils.dp2px(getContext(), 6), 0);
            textView.setTextSize(12);
            textView.setText(index.get(i));
            textView.setTextColor(Color.WHITE);
            textView.setGravity(Gravity.CENTER);
            textView.setClickable(true);
            textView.setOnTouchListener(new IndexTouchListener(i));
            ll_index.addView(textView);
        }
        return this;
    }


    private List<CarType> hotDatas;
    private AdapterView.OnItemClickListener l;

    public IndexExpandableListView setHeader(List<CarType> datas) {
        hotDatas = datas;
        View headerView = View.inflate(getContext(), R.layout.layout_expandable_header, null);
        FixedHeightGridView heightGridView = (FixedHeightGridView) headerView.findViewById(R.id.fhg_header);
        heightGridView.setOnItemClickListener(l);
        elv.addHeaderView(headerView);
        heightGridView.setAdapter(new CommonAdapter<CarType>(getContext(), R.layout.list_item_hot_header, datas) {
            @Override
            protected void convert(ViewHolder viewHolder, CarType item, int position) {
                viewHolder.setText(R.id.tv_hot, item.name);
                Glide.with(getContext()).load(ConstantUtil.IMAGE_URL + item.image).into((ImageView) viewHolder.getView(R.id.iv_hot));
            }
        });
        return this;
    }

    public void setAdapter(BaseExpandableListAdapter adapter) {
        elv.setAdapter(adapter);
        int groupCount = adapter.getGroupCount();
        // 默认展开所有分组
        for (int i = 0; i < groupCount; i++) {
            elv.expandGroup(i);
        }
    }

    /**
     * 设置child点击事件
     *
     * @param l
     */
    public void setOnChildClickListener(ExpandableListView.OnChildClickListener l) {
        elv.setOnChildClickListener(l);
    }

    /**
     * 设置child点击事件
     *
     * @param l
     */
    public void setOnItemClickListener(ExpandableListView.OnItemClickListener l) {
        this.l = l;
    }

    private class IndexTouchListener implements OnTouchListener {

        private int position;

        public IndexTouchListener(int position) {
            this.position = position;
        }


        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
                tvIndexChar.setVisibility(View.GONE);
            } else {

                tvIndexChar.setVisibility(View.VISIBLE);
                if (position == -1) {
                    tvIndexChar.setText("热门");
                    elv.setSelection(0);
                } else {

                    tvIndexChar.setText(indexList.get(position));
                    elv.setSelectedGroup(position);
                }
            }
            return false;
        }
    }

}
